/* 
 * Copyright notice:
 * This file has been taken from the example Google's Chrome to phone extension,
 * and adapted to our needs. (downloaded 17/05/2012 15:15)
 * (see http://http://chrometophone.googlecode.com/svn/trunk/android/src/com/google/android/apps/chrometophone/C2DMReceiver.java)
 */
package at.fhj.app.C2DM;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import at.fhj.app.MainActivity;
import at.fhj.app.R;

import com.google.android.c2dm.C2DMBaseReceiver;

public class C2DMReceiver extends C2DMBaseReceiver {

    private static final String TAG = "FH2go_C2DMBaseReceiver";
    
    private static final String MESSAGETYPE_SCHEDULE_UPDATE = "SCHEDULE_UPDATE";
    
    public C2DMReceiver() {
        super(C2DMDeviceRegistrar.SENDER_ID);
    }

    @Override
    public void onRegistered(Context context, String registration) {
	C2DMDeviceRegistrar.registerWithServer(context, registration);
    }

    @Override
    public void onUnregistered(Context context) {
        SharedPreferences prefs = C2DMPrefs.get(context);
        String deviceRegistrationID = prefs.getString("deviceRegistrationID", null);
        C2DMDeviceRegistrar.unregisterWithServer(context, deviceRegistrationID);
    }

    @Override
    public void onError(Context context, String errorId) {
        if (errorId == null)
            errorId = "<NULL>";
        
        // This method only gets called on registration errors (only on registration, not when unregistering)...

	Intent updateStatusIntent = new Intent(C2DMDeviceRegistrar.C2DM_REGISTRATION_UPDATE_INTENT);
	updateStatusIntent.putExtra(C2DMDeviceRegistrar.C2DM_REGISTRATION_UPDATE_INTENT_EXTRA_STATUSMESSAGE, context.getString(R.string.msg_errorc2dmregister_googleerror) + errorId);
	updateStatusIntent.putExtra(C2DMDeviceRegistrar.C2DM_REGISTRATION_UPDATE_INTENT_EXTRA_STATUS, false);  
	context.sendBroadcast(updateStatusIntent);
	
        Log.e(TAG, "Error occured: " + errorId);
    }

    @Override
    public void onMessage(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String messageType = "";            
            String additionalInfo = "";            

            if (extras.containsKey("MessageType"))
        	messageType = extras.getString("MessageType");
            if (extras.containsKey("AdditionalInfo"))
        	additionalInfo = extras.getString("AdditionalInfo");
            
            handleReceivedMessage(messageType, additionalInfo);
            
            Log.i(TAG, "Received C2DM message. MessageType: " + messageType + ", AddInfo: " + additionalInfo);
        }
    }
    
    private void handleReceivedMessage(String messageType, String additionalInfo) {
	if (messageType.equals(MESSAGETYPE_SCHEDULE_UPDATE)) {
	    handleReceivedMessage_ScheduleUpdate(additionalInfo);
	}
    }
    
    private void handleReceivedMessage_ScheduleUpdate(String additionalInfo) {
	String ns = Context.NOTIFICATION_SERVICE;
	NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
	
	int icon = R.drawable.launcher_gray;
	CharSequence tickerText = "Schedule has updates. Refresh on next use.";
	long when = System.currentTimeMillis();

	Notification notification = new Notification(icon, tickerText, when);
	
	Context context = getApplicationContext();
	CharSequence contentTitle = "FH2go";
	CharSequence contentText = "Schedule has updated. Please refresh on next use.";
	Intent notificationIntent = new Intent(this, MainActivity.class);
	PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

	notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
	
	int scheduleUpdateId = 1;

	mNotificationManager.notify(scheduleUpdateId, notification);
    }
}
